/*

  Photometry

  Copyright © 2010 - 2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "munipack.h"
#include <wx/wx.h>
#include <wx/log.h>
#include <wx/filename.h>

using namespace std;


bool Munipack::photometry(MuniCmdLineParser& cmd)
{
  MuniProcess *action = new MuniProcess(&pipe,"photometry");
  //MuniProcess *action = new MuniProcess(&pipe,"cat");
  pipe.push(action);

  wxString a;

  if( cmd.Found("f",&a) || cmd.Found("fwhm",&a) )
    action << ("FWHM = " + a);

  if( cmd.Found("t",&a) || cmd.Found("threshold",&a) )
    action << ("THRESHOLD = " + a);

  if( cmd.Found("phpadu",&a) )
    action << ("PHPADU = " + a);

  if( cmd.Found("readns",&a) )
    action << ("READ NOISE = " + a);

  if( cmd.Found("saturation",&a) )
    action << ("SATURATION = " + a);

  if( cmd.Found("lothresh",&a) )
    action << ("LOWER THRESHOLD = " + a);

  if( cmd.Found("shrplo",&a) )
    action << ("SHARP LOWER = " + a);

  if( cmd.Found("shrphi",&a) )
    action << ("SHARP HIGHER = " + a);

  if( cmd.Found("rndlo",&a) )
    action << ("ROUND LOWER = " + a);

  if( cmd.Found("rndhi",&a) )
    action << ("ROUND HIGHER = " + a);

  for(size_t i = 1; i < cmd.GetParamCount(); i++) {

    wxString par(cmd.GetParam(i));
    int p = par.Find(",");

    wxString file;
    if( p == wxNOT_FOUND ) {
      file = par;
      action << ("OUTPUT = '" + file + "'");
    }
    else {
      file = par.Mid(0,p);
      action << ("OUTPUT = '" + par.Mid(p+1) + "'");
    }
    action << ("FILE = '" + file + "'");

    // check writability of the file
    /*
    wxFileName f(file);
    if( f.IsFileWritable() )
      action << ("FILE = '" + file + "'");
    else
      wxLogWarning("File `" + file + "' is not writable. Skipped.");
    */
  }

  pipe.Start();

  return true;
}
